
import 'package:flutter/material.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/storage_manager.dart';
import 'package:s_mankey/provider/view_state_model.dart';
import 'package:s_mankey/service/smonkey_repository.dart';

class SearchEnterpriseVModel extends ViewStateModel{

  bool showResults = false; //显示搜索结果
  String keyword;  //搜索关键字
  List keyList = []; //搜索记录
  List datas= [];

  Map<String, dynamic> options = {}; //请求参数
//  SearchEnterpriseVModel({this.options});

//  Future initData ({String keyword}) async {
//    setBusy();
//    try {
//      datas = await SmonkeysRepository.fetchSearchEnterpeise(options: {'company_title': keyword});
//      this.showResults =false;
//      setIdle();
//    } catch (e, s){
//      setError(e,s);
//    }
//  }

  ///是否显示搜索结果
  setShowResults(bool show){
    this.showResults = show;
    notifyListeners();
  }

  ///设置搜索关键字并搜索
  setkeyword(String text){
   if(text.isNotEmpty)this.keyList.add(text);
    this.keyword = text;
    this.showResults = true;
    notifyListeners();
    this.saveKeyList(this.keyList);
  }

  ///设置请求参数
  setOptions(Map<String, dynamic> options){
    this.options = options;
  }

  ///获取搜索关键字记录
  getKeyList(){
    this.keyList = StorageManager.localeStorage.getItem(AppGlobalConfig.SEARCHENTERPRISE)?? [];
  }

  /// 保存搜索记录到本地
  saveKeyList(List keyList){
    StorageManager.localeStorage.setItem(AppGlobalConfig.SEARCHENTERPRISE, keyList);
  }

  /// 清除搜索记录
  cleanKeyList(){
    this.keyList = [];
    notifyListeners();
    StorageManager.localeStorage.setItem(AppGlobalConfig.SEARCHENTERPRISE, null);
  }

}